/*
 * Copyright (c) Facebook, Inc. and its affiliates.
 *
 * This source code is licensed under the MIT license found in the
 * LICENSE file in the root directory of this source tree.
 */

package com.facebook.imagepipeline.platform;

import com.facebook.imagepipeline.memory.BitmapPool;
import com.facebook.imageutils.BitmapUtil;
import com.facebook.imagepipeline.util.Pools;
import ohos.media.image.ImageSource;
import ohos.media.image.PixelMap;
import ohos.media.image.common.PixelFormat;

import javax.annotation.concurrent.ThreadSafe;

/**
 * Bitmap decoder for ART VM (Android O and up).
 */
@ThreadSafe
public class OreoDecoder extends DefaultDecoder {

    public OreoDecoder(BitmapPool bitmapPool, int maxNumThreads, Pools.SynchronizedPool decodeBuffers) {
        super(bitmapPool, maxNumThreads, decodeBuffers);
    }

    @Override
    public int getBitmapSize(final int width, final int height, final  ImageSource.DecodingOptions options) {
        // If the color is wide gamut but the Bitmap Config doesn't use 8 bytes per pixel, the size of
        // the bitmap
        // needs to be computed manually to get the correct size.
        return hasColorGamutMismatch(options)
                ? width * height * 8
                : BitmapUtil.getSizeInByteForBitmap(width, height, options.desiredPixelFormat);
    }

    /**
     * Check if the color space has a wide color gamut and is consistent with the Bitmap config
     */
    private static boolean hasColorGamutMismatch(final ImageSource.DecodingOptions options) {
        return options.desiredColorSpace != null
               // && options.desiredColorSpace.isWideGamut()
                && options.desiredPixelFormat != PixelFormat.UNKNOWN;
    }
}
